﻿/*
 * Copyright (c) 2009, Thiago M. Sayao <thiago.sayao@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the author nor the names of its contributors may be 
 *    used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using Resources;

namespace DotNetBurner
{
    public class ImageGenerator
    {
        const float Width = 82.0000f;
        const float Height = 18.0000f;
        const float BorderWidth = 1f;
        readonly Color _backColor = ColorTranslator.FromHtml("#fcaf3e");
        readonly Color _fontColor = Color.White;
        readonly Color _voteFontColor = Color.White;
        readonly Color _borderColor = ColorTranslator.FromHtml("#ce5c00");
        readonly Color _voteBackColor = ColorTranslator.FromHtml("#f57900");

        public Bitmap Generate(int votes)
        {
            const int countWidthBuffer = 6;
            const string fontName = "Trebuchet MS";
            const int fontSize = 14;

            string strVotes = votes.ToString();
            string text = (votes == 0) ? Strings.Submit.ToLowerInvariant() : Strings.VoteIt;

            Bitmap img = new Bitmap((int)Width, (int)Height, PixelFormat.Format24bppRgb);

            using (Graphics g = Graphics.FromImage(img))
            {
                g.CompositingQuality = CompositingQuality.HighSpeed;
                g.InterpolationMode = InterpolationMode.Bilinear;
                g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
                g.SmoothingMode = SmoothingMode.HighSpeed;

                //draw border
                using (Brush borderBrush = new SolidBrush(_borderColor))
                {
                    g.FillRectangle(borderBrush, 0, 0, Width, Height);
                }

                //draw background
                using (Brush textBackgroundBrush = new SolidBrush(_backColor))
                {
                    g.FillRectangle(textBackgroundBrush, BorderWidth, BorderWidth, (Width - (BorderWidth * 2)), (Height - (BorderWidth * 2)));
                }

                using (Font font = new Font(fontName, fontSize, FontStyle.Bold, GraphicsUnit.Pixel))
                {
                    SizeF voteSize = g.MeasureString(strVotes, font);

                    float txtWidth = Width - (voteSize.Width + countWidthBuffer + (BorderWidth * 2));

                    using (Brush txtFgBrush = new SolidBrush(_fontColor))
                    {
                        SizeF textSize = g.MeasureString(text, font);

                        float x = ((txtWidth - textSize.Width) / 2) + BorderWidth;
                        float y = (Height - textSize.Height) / 2;

                        g.DrawString(text, font, txtFgBrush, x, y);
                    }

                    using (Brush voteBackBrush = new SolidBrush(_voteBackColor))
                    {
                        g.FillRectangle(voteBackBrush, (txtWidth + BorderWidth), BorderWidth, (voteSize.Width + countWidthBuffer), (Height - (BorderWidth * 2)));
                    }

                    using (Brush countForegroundBrush = new SolidBrush(_voteFontColor))
                    {
                        float x = ((voteSize.Width + countWidthBuffer - voteSize.Width) / 2) + BorderWidth + txtWidth;
                        float y = (Height - voteSize.Height) / 2;

                        g.DrawString(strVotes, font, countForegroundBrush, x, y);
                    }
                }
                    
                return img;
            }
        }
    }
}
